package com.zhenmyan.secdeal.exception;

import com.zhenmyan.secdeal.Enum.ResponseEnum;
import com.zhenmyan.secdeal.vo.Response;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * description: 异常处理类
 *
 * @author 颜真明
 * @date 2023/5/9  15:00
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Response ExceptionHandler(Exception e) {
        if (e instanceof GlobalException) {
            GlobalException globalException = (GlobalException)e;
            return Response.error(globalException.getResponseEnum());
        } else if (e instanceof BindException) {
            BindException bindException = (BindException)e;
            Response response = Response.error(ResponseEnum.BIND_EXCEPTION);
            response.setMessage("参数校验异常：" + bindException.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return response;
        }
        return Response.error(ResponseEnum.ERROR);
    }

}
